/**
 * 
 */
package com.xth8013.as.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xth8013.as.dao.UserDao;
import com.xth8013.as.domain.User;
import com.xth8013.as.exception.ServiceLogicException;
import com.xth8013.as.service.SecurityService;
import com.xth8013.as.util.MD5Util;
import com.xth8013.as.util.ObjectUtils;
import com.xth8013.as.util.StringUtils;


/**
 * 
 * @Description:
 * @author: tianhua.xie
 * @Date: 2017年2月9日 下午9:53:07 
 * 
 */
@Service("securityService")
@Transactional
public class SecurityServiceImpl implements SecurityService {

	@Autowired
	private UserDao userDao;

	@Override
	public User login(String account, String pwd) {

		if (StringUtils.isBlank(account)) {
			throw new ServiceLogicException("登录账户不能为空");
		}
		if (StringUtils.isBlank(pwd)) {
			throw new ServiceLogicException("登录密码不能为空");
		}

		User dbUser = userDao.selectByAccount(account);

		if (ObjectUtils.isBlank(dbUser)) {
			throw new ServiceLogicException("该用户账号不存在");
		}

		if ("0".equals(dbUser.getUserStatus())) {
			throw new ServiceLogicException("该用户账号已失效");
		}

		String md5Pwd = null;
		try {
			md5Pwd = MD5Util.md5Encode(pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(md5Pwd);

		if (!md5Pwd.equals(dbUser.getUserPwd())) {
			throw new ServiceLogicException("账号或密码不正确");
		}

		return dbUser;
	}

	@Override
	public User register(User user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isExistUser(String account) {

		if (StringUtils.isBlank(account)) {
			return false;
		}

		User user = userDao.selectByAccount(account);
		if (ObjectUtils.isBlank(user)) {
			return false;
		}

		return true;
	}

}
